package cn.itcast.service.impl;

import cn.itcast.dao.AccountDao;
import cn.itcast.domain.Account;
import cn.itcast.service.AccountService;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao accountDao;
    @Autowired
    private RedisTemplate redisTemplate;

    @Transactional(propagation = Propagation.REQUIRED)
    public void transfer(Integer inId, Integer outId, Double money) throws Exception {
        accountDao.inMoney(inId, money);
        System.out.println(1 / 0);
        accountDao.outMoney(outId, money);
    }


    @Transactional
    public List<Account> findAll() {
        List<Account> list = accountDao.findAll();
        redisTemplate.opsForValue().set("listx", list);
        return list;
    }

    public Account findById(Insert id) {
        Account account = accountDao.findById(id);
        return account;
    }
}
